package com.market.service.impl;

import com.market.common.exception.ServiceException;
import com.market.common.vo.ResponseCode;
import com.market.entity.CommercialTenant;
import com.market.mapper.CommercialTenantMapper;
import com.market.service.CommercialTenantService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CommercialTenantServiceImpl implements CommercialTenantService {
    @Resource
    CommercialTenantMapper commercialTenantMapper;

    /**
     * 新增商户
     *
     * @param commercialTenant
     * @return
     * @throws ServiceException
     */
    @Override
    public int addCommercialTenant(CommercialTenant commercialTenant) throws ServiceException {
        int count = 0;
        List<CommercialTenant> select = commercialTenantMapper.select();
        for (CommercialTenant commercialTenants : select) {
            if (commercialTenants.getCommercialTenantName() != commercialTenant.getCommercialTenantName()) {
                int insert = commercialTenantMapper.insert(commercialTenant);
                if (insert > 0) {
                    count = insert;
                    throw new ServiceException(ResponseCode.SUCCESS);
                } else {
                    throw new ServiceException(ResponseCode.ERROR);
                }
            } else {
                throw new ServiceException(ResponseCode.ERROR);
            }
        }
        return count;
    }

    /**
     * 删除
     *
     * @param commercialTenantId
     * @return
     */
    @Override
    public int delCommercialTenant(Long commercialTenantId) throws ServiceException {
        int delect = commercialTenantMapper.delect(commercialTenantId);
        if (delect > 0) {
            throw new ServiceException(ResponseCode.SUCCESS);
        } else {
            throw new ServiceException(ResponseCode.ERROR);
        }
    }


    /**
     * 查询所有商户信息
     *
     * @return
     */
    @Override
    public List<CommercialTenant> findCommercialTenant() throws ServiceException {
        List<CommercialTenant> selects = commercialTenantMapper.selects();
        if (selects.size() > 0) {
            return selects;
        } else {
            throw new ServiceException(ResponseCode.ERROR);
        }

    }

}
